﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Security;
using System.Windows.Forms;
using BlueMarineWindowsFormsClient.ManagerService;

namespace BlueMarineWindowsFormsClient
{
    public partial class frmEquipmentSelect : Form
    {
        private readonly ManagerServiceClient _client;
        private readonly frmHotelManager _parent;
        private List<EquipmentDetail> _data;

        public frmEquipmentSelect(frmHotelManager pr, string username, string password)
        {
            _data = new List<EquipmentDetail>();
            _parent = pr;
            InitializeComponent();

            _client = new ManagerServiceClient("WSHttpBinding_IManagerService")
                          {
                              ClientCredentials =
                                  {
                                      UserName = {UserName = username, Password = password},
                                      ServiceCertificate =
                                          {
                                              Authentication =
                                                  {CertificateValidationMode = X509CertificateValidationMode.None}
                                          }
                                  }
                          };
        }

        private void Equipment_Load(object sender, EventArgs e)
        {
            _data = _client.EquipmentDetailsGet(true).ToList();
            var source = new BindingSource {DataSource = _data};
            dtaGrdEquipment.DataSource = source;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            var reList = new List<EquipmentDetail>();
            DataGridViewRowCollection rows = dtaGrdEquipment.Rows;
            for (int i = 0; i < rows.Count; i++)
            {
                if (rows[i].Cells[0].Value != null)
                {
                    reList.Add(_data[i]);
                }
            }
            foreach (EquipmentDetail re in reList)
            {
                _parent.Temp_EquipmentDetails.Add(re);
            }
            _parent.viewRoomTypeEquipment();
            Dispose();
        }
    }
}